import redis


class RedisCon:
    def __init__(self):
        self.conn = redis.Redis(host='127.0.0.1', port='6379')

    # time是过期时间
    # 存redis
    def set_str(self, key, value, time=0):
        if time == 0:
            self.conn.set(key, value)
        else:
            self.conn.setex(key, time, value)  # time以秒为单位

    # 取redis
    def get_str(self, key):
        # 根据key找value
        value = self.conn.get(key)
        if value:
            value = str(value, encoding='utf8')
        return value

    # 删除redis
    def del_str(self, key):
        self.conn.delete(key)


r = RedisCon()


# r.set_str('name','cbf',30)
# r.del_str('name')
# print(r.get_str('name'))
